/* gpcurfmt.p - PROGRAM TO FORMAT AN AMOUNT ACCORDING TO ROUNDING METHOD   */
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.            */
/* $Id:: gpcurfmt.p 2366 2010-11-10 01:47:19Z zzh                          $: */
/*V8:ConvertMode=Maintenance                                               */
/* Revision: 8.5      LAST MODIFIED: 11/10/95   By: TAF,MWD *J053*         */
/* Revision: 9.1      LAST MODIFIED: 08/14/00   By: *N0L1* Mark Brown      */
/* Revision: 9.1      LAST MODIFIED: 08/16/00   By: *N0LG* Katie Hilbert   */
/* Revision: 1.5        BY: Paul Donnelly (SB)  DATE: 06/26/03  ECO: *Q00F* */
/* Revision: 1.7        BY: Ken Casey           DATE: 03/07/07  ECO: *R0C6* */
/* $Revision: 1.8 $     BY: Jean Miller         DATE: 11/06/07  ECO: *P5YL* */
/*-Revision end---------------------------------------------------------------*/
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
/***************************************************************************/
/* This function generates the formatting for a given currency amount. It  */
/* takes a format string supplied by a calling program, modifies it, and   */
/* returns the modified format to be used by the calling program.  It is   */
/* currently set up to handle currencies defined like: (ZZZ,ZZZ.99) ,      */
/* ->>>,>>>,>>9.99 and *****,***,**9.99.  This function does not handle    */
/* currency punctuation: Punctuation is handled separately by the session  */
/* parameter 'numeric-format' setting, set at the time that the base       */
/* currency is determined.  This function handles from 0-3 decimal places  */
/* and presumes that it starts with a 2-decimal format.                    */
/* All modifications to this file should also be made to gpcrfmt.i.        */
/***************************************************************************/

{us/bbi/mfdeclre.i }

define input-output parameter fmtstr as character format "x(30)"
     no-undo.
define input parameter rndmthd like rnd_rnd_mthd no-undo.
define variable new_fmt as character format "x(30)" no-undo.
define variable fmtlen as integer no-undo.
define variable addstr as character no-undo.
define variable decimal_loc as integer no-undo.
define variable l_fmtstr1 as character format "x(30)" no-undo.

find first rnd_mstr where rnd_rnd_mthd = rndmthd no-lock no-error.

if not available rnd_mstr then do:
   {us/bbi/pxmsg.i &MSGNUM=863 &ERRORLEVEL=4}   /* ROUND METHOD DOES NOT EXIST */
end. /* IF NOT AVAILABLE RND_MSTR */
else do:

   /* Force the initial currency format to have only 2 decimal places */
   /* which are formatted as ".99" as opposed to ".9<" or ".<<".      */
   /* Using a format character of "9" will insure that the digits in  */
   /* the decimal places display even if they have a value of 0.      */
   /* If no decimal point is found then just return without updating  */
   /* the format string.                                              */

   decimal_loc = index(fmtstr,".").
   if decimal_loc = 0 then return.
   /* Check if parametr 1 is already rounded to the required round method */
   /*as mentioned in parameter2;if so no formatting required,return as is */
   l_fmtstr1 = substring(fmtstr,decimal_loc + 1,length(fmtstr)).
   if length(l_fmtstr1) = integer(rndmthd)
      then return.
   if (fmtstr begins "(") then
      fmtstr = substring(fmtstr,1,decimal_loc) + "99)".
   else
      fmtstr = substring(fmtstr,1,decimal_loc) + "99".

   fmtlen = length(fmtstr).

   /* THREE DECIMAL DISPLAY: ADDS ONE DECIMAL, */
   /* ELIMINATES A HIGH ORDER DIGIT            */
   if (rnd_unit < .01) then do:

      addstr = "9".

      if (fmtstr begins "(") then  do:
         new_fmt = substring(fmtstr,1,1).
         if (substring(fmtstr,3,1) = ",") then
            new_fmt = new_fmt + substring(fmtstr,2,1) +
                      substring(fmtstr,4,(fmtlen - 4)).
         else
            new_fmt = new_fmt + substring(fmtstr,3,(fmtlen - 3)).
         new_fmt = new_fmt + addstr + ")".
      end.

      else do:
         new_fmt = substring(fmtstr,1,1).
         if (substring(fmtstr,3,1) = ",") then
            new_fmt = new_fmt + substring(fmtstr,2,1) +
                      substring(fmtstr,4,(fmtlen - 2)).
         else
            new_fmt = new_fmt + substring(fmtstr,3,(fmtlen - 1)).
         new_fmt = new_fmt + addstr.
      end.

   end. /* IF (RND_UNIT < .01 */

   /* TWO DECIMAL DISPLAY: DO NOT CHANGE FORMAT STRING */
   else if (rnd_unit < .1) then
      new_fmt = fmtstr.

   /* ONE DECIMAL DISPLAY: ADD ONE HIGH ORDER DIGIT AND REMOVE */
   /* ONE DECIMAL DIGIT FROM FORMAT STRING                     */
   else if (rnd_unit < 1) then do:

      if (substring(fmtstr,2,1) = "z") then
         addstr = "z".
      else if (substring(fmtstr,2,1) = "*") then
         addstr = "*".
      else
         addstr = ">".

      if (fmtstr begins "(") then
         new_fmt = substring(fmtstr,1,1) + addstr +
                   substring(fmtstr,2,(fmtlen - 3)) + ")".
      else
         new_fmt = substring(fmtstr,1,1) + addstr +
                   substring(fmtstr,2,(fmtlen - 2)).

   end.

   /* ZERO DECIMAL DISPLAY: ADD THREE HIGH ORDER DIGITS AND    */
   /* REMOVE TWO DECIMAL DIGITS AND DECIMAL FROM FORMAT STRING */
   else do:

      if (substring(fmtstr,2,1) = "z") then
         addstr = "zzz".
      else if (substring(fmtstr,2,1) = "*") then
         addstr = "***".
      else
         addstr = ">>>".

      if (fmtstr begins "(") then
         new_fmt = substring(fmtstr,1,1) + addstr +
                   substring(fmtstr,2,(fmtlen - 5)) + ")".
      else
         new_fmt = substring(fmtstr,1,1) + addstr +
                   substring(fmtstr,2,(fmtlen - 4)).

   end.

   fmtstr = new_fmt.

end. /* IF AVAILABLE RND_MSTR*/
